package com.aplink.generic.pojo.googlemaps;

import java.lang.reflect.Type;
import java.util.ArrayList;

import com.google.android.gms.maps.model.LatLng;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

public class DirectionsLeg implements JsonDeserializer<DirectionsLeg> {
	private Distance mDistance;
	private Duration mDuration;
	private String mEndAddress;
	private LatLng mEndLocation;
	private String mStartAddress;
	private LatLng mStartLocation;
	private ArrayList<DirectionsStep> mSteps;

	public DirectionsLeg() {
		mSteps = new ArrayList<>();
	}

	@Override
	public DirectionsLeg deserialize(JsonElement jsonElement, Type type,
	        JsonDeserializationContext context) throws JsonParseException {
		JsonObject jsonObject = jsonElement.getAsJsonObject();
		mDistance = context.deserialize(jsonObject.get("distance"),
		        Distance.class);
		mDuration = context.deserialize(jsonObject.get("duration"),
		        Duration.class);
		mStartAddress = jsonObject.get("start_address").getAsString();
		mStartLocation = new LatLng(jsonObject
		        .getAsJsonObject("start_location").get("lat").getAsDouble(),
		        jsonObject.getAsJsonObject("start_location").get("lng")
		                .getAsDouble());
		mEndAddress = jsonObject.get("end_address").getAsString();
		mEndLocation = new LatLng(jsonObject.getAsJsonObject("end_location")
		        .get("lat").getAsDouble(), jsonObject
		        .getAsJsonObject("end_location").get("lng").getAsDouble());
		mSteps = context.deserialize(jsonObject.get("steps"), ArrayList.class);
		return this;
	}

	public Distance getDistance() {
		return mDistance;
	}

	public Duration getDuration() {
		return mDuration;
	}

	public String getEndAddress() {
		return mEndAddress;
	}

	public LatLng getEndLocation() {
		return mEndLocation;
	}

	public String getStartAddress() {
		return mStartAddress;
	}

	public LatLng getStartLocation() {
		return mStartLocation;
	}

	public ArrayList<DirectionsStep> getSteps() {
		return mSteps;
	}

	public void setDistance(Distance distance) {
		mDistance = distance;
	}

	public void setDuration(Duration duration) {
		mDuration = duration;
	}

	public void setEndAddress(String endAddress) {
		mEndAddress = endAddress;
	}

	public void setEndLocation(LatLng endLocation) {
		mEndLocation = endLocation;
	}

	public void setStartAddress(String startAddress) {
		mStartAddress = startAddress;
	}

	public void setStartLocation(LatLng startLocation) {
		mStartLocation = startLocation;
	}

	public void setSteps(ArrayList<DirectionsStep> steps) {
		mSteps = steps;
	}
}
